package gui;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.awt.*;
import javax.swing.*;

import model.WriteExcelToDB;
import model.WriteInformationInToDB;

import java.awt.event.*;

public class YFileChooser implements ActionListener {
	Map<String, Double> per = new HashMap<>();
	JFrame frame = new JFrame("文件选择");
	JTabbedPane tabPane = new JTabbedPane();// 选项卡布局
	Container con = new Container();// 布局1
	Container con1 = new Container();// 布局2
	JLabel label2 = new JLabel("请选择文件：");
	JLabel label3 = new JLabel("该成绩所属平台：");
	JLabel label4 = new JLabel("设置权重：");
	JLabel label5 = new JLabel("权重合成列列名：");
	JLabel label6 = new JLabel("请注意权值相加不能大于1");
	JTextField text2 = new JTextField();
	JTextField text3 = new JTextField();
	JTextArea text4 = new JTextArea();
	JTextField text5 = new JTextField();
	JTextField text6 = new JTextField();
	JTextField textName = new JTextField();
	JScrollPane spane = new JScrollPane();
	JButton button2 = new JButton("...");
	JButton button3 = new JButton("OK");
	JButton button4 = new JButton("合成列");
	JButton button5 = new JButton("回撤操作");
	JButton outbutton = new JButton("导出");
	JFileChooser jfc = new JFileChooser();// 文件选择器

	YFileChooser() {
		jfc.setCurrentDirectory(new File("d:\\"));// 文件选择器的初始目录定为d盘
		// 下面两行是取得屏幕的高度和宽度
		double lx = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		double ly = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		frame.setLocation(new Point((int) (lx / 2) - 150, (int) (ly / 2) - 150));// 设定窗口出现位置
		frame.setSize(600, 600);// 设定窗口大小
		frame.setContentPane(tabPane);// 设置布局
		label2.setBounds(25, 20, 120, 20);
		label3.setBounds(25, 50, 120, 20);
		label4.setBounds(25, 80, 120, 20);
		label5.setBounds(25, 400, 160, 20);
		label6.setBounds(25, 100, 200, 20);
		text2.setBounds(160, 20, 120, 20);
		text3.setBounds(160, 50, 120, 20);
		// text4.setBounds(10,400,100,100);
		text5.setBounds(160, 80, 120, 20);
		text6.setBounds(160, 400, 120, 20);
		textName.setBounds(400, 20, 120, 20);
		button2.setBounds(300, 20, 50, 20);
		button3.setBounds(300, 80, 51, 20);
		button4.setBounds(300, 400, 75, 20);
		button5.setBounds(400, 400, 100, 20);
		outbutton.setBounds(500, 400, 75, 20);
		button2.addActionListener(this);// 添加事件处理
		button3.addActionListener(this);
		button4.addActionListener(this);
		button5.addActionListener(this);
		outbutton.addActionListener(this);
		spane.setBounds(15, 125, 470, 250);
		con.add(spane);
		con.add(label2);
		con.add(label3);
		con.add(label4);
		con.add(label5);
		con.add(label6);
		con.add(text2);
		con.add(text3);
		con.add(text5);
		con.add(text6);
		con.add(textName);

		con.add(button2);
		con.add(button3);
		con.add(button4);
		con.add(button5);
		con.add(outbutton);
		con.add(jfc);
		spane.setViewportView(text4);
		tabPane.add("目录/文件选择", con);// 添加布局1
		frame.setVisible(true);// 窗口可见
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);// 只关闭当前窗口

	}
	@Override
	public void actionPerformed(ActionEvent e) {// 事件处理

		if (e.getSource().equals(button2)) {
			jfc.setFileSelectionMode(0);// 设定只能选择到文件
			int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
			if (state == 1) {
				return;// 撤销则返回
			} else {
				File f = jfc.getSelectedFile();// f为选择到的文件
				text2.setText(f.getAbsolutePath());

			}
		}
		if (e.getSource().equals(button3)) {
			String setPos = text3.getText();
			String filename = text2.getText();
			String content = text5.getText();
			double weight = Double.parseDouble(content);
			try {
				String tablename = textName.getText();
				text4.setText(WriteExcelToDB.writeExcl(tablename, setPos, filename, setPos) + "\n"
						+ WriteExcelToDB.findErr(tablename, setPos));
				per.put(setPos, weight);

			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		if (e.getSource().equals(button4)) {
			String columnName = text6.getText();
			String tablename = textName.getText();
			double percou = 0;
			for (Entry<String, Double> en : per.entrySet()) {
				percou += en.getValue();
			}
			System.out.println(percou);
			System.out.println(per.size());
			if (percou == 1) {
				WriteExcelToDB.accuAccordPercent(tablename, columnName, per);
			} else {
				JOptionPane.showMessageDialog(null, "权值相加大于1或小于1 请按回撤按钮", "有错啦", JOptionPane.CLOSED_OPTION);
				try {
					WriteInformationInToDB.deletetable(tablename);
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		if (e.getSource().equals(button5)) {
			new Menu().setVisible(true);
			;
			frame.setVisible(false);
		}
		if (e.getSource().equals(outbutton)) {

			String tablename = textName.getText();
			ComposeExcel ce = new ComposeExcel();
			ce.jTextField2.setText(tablename);
			ce.setVisible(true);
			frame.setVisible(false);
		}
	}

	public static void main(String[] args) {
		new YFileChooser();
	}
}